//
//  GFLikeFoodViewController.m
//  GoodFood
//
//  Created by dllo on 15/11/2.
//  Copyright © 2015年 荣伟峰. All rights reserved.
//

#import "GFLikeFoodViewController.h"

@interface GFLikeFoodViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *likeFoodTable;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, copy) NSString *url;

@end

@implementation GFLikeFoodViewController


-(void)creatTableView {
    
    self.likeFoodTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight - 64) style:UITableViewStylePlain];
    self.likeFoodTable.delegate = self;
    self.likeFoodTable.dataSource = self;
    self.likeFoodTable.rowHeight = ScreenWidth / 10 + 20;
    
    [self.view addSubview:self.likeFoodTable];
    self.likeFoodTable.tableFooterView = [[UIView alloc] init];
    [self.likeFoodTable registerClass:[GFLikeFoodTableViewCell class] forCellReuseIdentifier:@"likeFood"];
    if (self.judge != 0) {
        self.likeFoodTable.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footRefresh:)];
        if (self.tempArr.count < 10) {
            self.likeFoodTable.footer.hidden = YES;
        }
        
    }
}

#pragma mark - 上拉加载
- (void)footRefresh:(MJRefreshAutoNormalFooter *)footer {
    self.likeFoodTable.footer.hidden = NO;
    [self.likeFoodTable.footer beginRefreshing];
    self.page++;
    NSString *followURL = [NSString stringWithFormat:@"http://api.meishixing.com/user/social/following/uid=%@&session_id=0000125e4d5a8b4f49f12cc7532f3485cb0a91&page=%ld", self.user_id, self.page];
    
    NSString * fansURL = [NSString stringWithFormat:@"http://api.meishixing.com/user/social/fans/uid=%@&session_id=0000125e4d5a8b4f49f12cc7532f3485cb0a91&page=%ld", self.user_id, self.page];
    
    if (self.judge == 1) {
        self.url = followURL;
    } else if (self.judge == 2) {
        self.url = fansURL;
    }
    [AFNetHandle GETWithURL:self.url body:nil httpHead:nil responseStyle:JSON success:^(id result) {
        NSArray *arr = result[@"result"];
        NSMutableArray *tempArr = [NSMutableArray array];
        for (NSDictionary *dic in self.tempArr) {
            [tempArr addObject:dic];
        }
        for (NSDictionary *dic in arr) {
            [tempArr addObject:dic];
        }
        self.tempArr = tempArr;
        [self.likeFoodTable reloadData];
        [self.likeFoodTable.footer endRefreshing];
    } fail:^(NSError *error) {
        
    }];
    
    

    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.page = 1;
    [self creatTableView];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.tempArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *tempArr = self.tempArr;
    NSDictionary *tempDic = [tempArr objectAtIndex:indexPath.row];
    GFLikeFoodTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"likeFood"];
    cell.tempDic = tempDic;
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *tempArr = self.tempArr;
    NSDictionary *tempDic = [tempArr objectAtIndex:indexPath.row];
    GFUserInfoViewController *userInfoVC = [[GFUserInfoViewController alloc] init];
    userInfoVC.user_id = tempDic[@"user_id"];
    [self.navigationController pushViewController:userInfoVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
